//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PrimeNumbers.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CPrimeNumbers::CPrimeNumbers (boost::shared_ptr<QuantLib::PrimeNumbers>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPrimeNumbers = NULL;
#endif
	_ppPrimeNumbers = &childNative;
    
}
Cephei::QL::Math::CPrimeNumbers::CPrimeNumbers (QuantLib::PrimeNumbers& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPrimeNumbers = NULL;
#endif
	_ppPrimeNumbers = new boost::shared_ptr<QuantLib::PrimeNumbers> (&childNative);
    
    _PrimeNumbersOwner = owner;
    
}

Cephei::QL::Math::CPrimeNumbers::CPrimeNumbers (CPrimeNumbers^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPrimeNumbers = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPrimeNumbers = new boost::shared_ptr<QuantLib::PrimeNumbers> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CPrimeNumbers::CPrimeNumbers (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPrimeNumbers = NULL;
#endif
	if (!t->IsSubclassOf(CPrimeNumbers::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CPrimeNumbers::CPrimeNumbers (QuantLib::Handle<QuantLib::PrimeNumbers>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPrimeNumbers = &childNative;
	_ppPrimeNumbers = &static_cast<boost::shared_ptr<QuantLib::PrimeNumbers>>(childNative.currentLink());
    
    _PrimeNumbersOwner = owner;
}
Cephei::QL::Math::CPrimeNumbers::CPrimeNumbers (QuantLib::Handle<QuantLib::PrimeNumbers> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPrimeNumbers = &childNative;
	_ppPrimeNumbers = &static_cast<boost::shared_ptr<QuantLib::PrimeNumbers>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CPrimeNumbers::CPrimeNumbers (QuantLib::PrimeNumbers childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPrimeNumbers = NULL;
#endif
	_ppPrimeNumbers = new boost::shared_ptr<QuantLib::PrimeNumbers> (new QuantLib::PrimeNumbers (childNative));
    
}
#endif

Cephei::QL::Math::CPrimeNumbers::~CPrimeNumbers ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPrimeNumbers != NULL)
    {
	    delete _ppPrimeNumbers;
        _ppPrimeNumbers = NULL;
    }
}
Cephei::QL::Math::CPrimeNumbers::!CPrimeNumbers ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPrimeNumbers != NULL)
    {
	    delete _ppPrimeNumbers;
    }
}
QuantLib::PrimeNumbers& Cephei::QL::Math::CPrimeNumbers::GetReference ()
{
    if (_ppPrimeNumbers == NULL) throw REFNEW NativeNullException ();
	return **_ppPrimeNumbers;
}
boost::shared_ptr<QuantLib::PrimeNumbers>& Cephei::QL::Math::CPrimeNumbers::GetShared ()
{
    if (_ppPrimeNumbers == NULL) throw REFNEW NativeNullException ();
	return *_ppPrimeNumbers;
}
QuantLib::PrimeNumbers* Cephei::QL::Math::CPrimeNumbers::GetPointer ()
{
    if (_ppPrimeNumbers == NULL) throw REFNEW NativeNullException ();
	return &**_ppPrimeNumbers;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PrimeNumbers>& Cephei::QL::Math::CPrimeNumbers::GetHandle ()
{
	if (_phPrimeNumbers == NULL)
	{
		_phPrimeNumbers = new Handle<QuantLib::PrimeNumbers> (*_ppPrimeNumbers);
	}
	return *_phPrimeNumbers;
}
#endif
bool Cephei::QL::Math::CPrimeNumbers::HasNative () 
{
	return (_ppPrimeNumbers != NULL);
}

UInt64 Cephei::QL::Math::CPrimeNumbers::Get (UInt64 absoluteIndex)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _absoluteIndex = (QuantLib::Size)ValueHelper::Convert (absoluteIndex); //a
    	QuantLib::BigNatural _rv = (QuantLib::BigNatural)(*_ppPrimeNumbers)->get ( _absoluteIndex );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

